1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.primitives;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.collect.testing.Helpers;
21  
22  import junit.framework.TestCase;
23  
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.List;
28  
29  /**
30   * Unit test for {@link Bytes}.
31   *
32   * @author Kevin Bourrillion
33   */
34  @GwtCompatible(emulated = true)
35  public class BytesTest extends TestCase {
36    private static final byte[] EMPTY = {};
37    private static final byte[] ARRAY1 = {(byte) 1};
38    private static final byte[] ARRAY234
39        = {(byte) 2, (byte) 3, (byte) 4};
40  
41    private static final byte[] VALUES =
42        { Byte.MIN_VALUE, -1, 0, 1, Byte.MAX_VALUE };
43  
44    public void testHashCode() {
45      for (byte value : VALUES) {
46        assertEquals(((Byte) value).hashCode(), Bytes.hashCode(value));
47      }
48    }
49  
50    public void testContains() {
51      assertFalse(Bytes.contains(EMPTY, (byte) 1));
52      assertFalse(Bytes.contains(ARRAY1, (byte) 2));
53      assertFalse(Bytes.contains(ARRAY234, (byte) 1));
54      assertTrue(Bytes.contains(new byte[] {(byte) -1}, (byte) -1));
55      assertTrue(Bytes.contains(ARRAY234, (byte) 2));
56      assertTrue(Bytes.contains(ARRAY234, (byte) 3));
57      assertTrue(Bytes.contains(ARRAY234, (byte) 4));
58    }
59  
60    public void testIndexOf() {
61      assertEquals(-1, Bytes.indexOf(EMPTY, (byte) 1));
62      assertEquals(-1, Bytes.indexOf(ARRAY1, (byte) 2));
63      assertEquals(-1, Bytes.indexOf(ARRAY234, (byte) 1));
64      assertEquals(0, Bytes.indexOf(
65          new byte[] {(byte) -1}, (byte) -1));
66      assertEquals(0, Bytes.indexOf(ARRAY234, (byte) 2));
67      assertEquals(1, Bytes.indexOf(ARRAY234, (byte) 3));
68      assertEquals(2, Bytes.indexOf(ARRAY234, (byte) 4));
69      assertEquals(1, Bytes.indexOf(
70          new byte[] { (byte) 2, (byte) 3, (byte) 2, (byte) 3 },
71          (byte) 3));
72    }
73  
74    public void testIndexOf_arrayTarget() {
75      assertEquals(0, Bytes.indexOf(EMPTY, EMPTY));
76      assertEquals(0, Bytes.indexOf(ARRAY234, EMPTY));
77      assertEquals(-1, Bytes.indexOf(EMPTY, ARRAY234));
78      assertEquals(-1, Bytes.indexOf(ARRAY234, ARRAY1));
79      assertEquals(-1, Bytes.indexOf(ARRAY1, ARRAY234));
80      assertEquals(0, Bytes.indexOf(ARRAY1, ARRAY1));
81      assertEquals(0, Bytes.indexOf(ARRAY234, ARRAY234));
82      assertEquals(0, Bytes.indexOf(
83          ARRAY234, new byte[] { (byte) 2, (byte) 3 }));
84      assertEquals(1, Bytes.indexOf(
85          ARRAY234, new byte[] { (byte) 3, (byte) 4 }));
86      assertEquals(1, Bytes.indexOf(ARRAY234, new byte[] { (byte) 3 }));
87      assertEquals(2, Bytes.indexOf(ARRAY234, new byte[] { (byte) 4 }));
88      assertEquals(1, Bytes.indexOf(new byte[] { (byte) 2, (byte) 3,
89          (byte) 3, (byte) 3, (byte) 3 },
90          new byte[] { (byte) 3 }
91      ));
92      assertEquals(2, Bytes.indexOf(
93          new byte[] { (byte) 2, (byte) 3, (byte) 2,
94              (byte) 3, (byte) 4, (byte) 2, (byte) 3},
95          new byte[] { (byte) 2, (byte) 3, (byte) 4}
96      ));
97      assertEquals(1, Bytes.indexOf(
98          new byte[] { (byte) 2, (byte) 2, (byte) 3,
99              (byte) 4, (byte) 2, (byte) 3, (byte) 4},
100         new byte[] { (byte) 2, (byte) 3, (byte) 4}
101     ));
102     assertEquals(-1, Bytes.indexOf(
103         new byte[] { (byte) 4, (byte) 3, (byte) 2},
104         new byte[] { (byte) 2, (byte) 3, (byte) 4}
105     ));
106   }
107 
108   public void testLastIndexOf() {
109     assertEquals(-1, Bytes.lastIndexOf(EMPTY, (byte) 1));
110     assertEquals(-1, Bytes.lastIndexOf(ARRAY1, (byte) 2));
111     assertEquals(-1, Bytes.lastIndexOf(ARRAY234, (byte) 1));
112     assertEquals(0, Bytes.lastIndexOf(
113         new byte[] {(byte) -1}, (byte) -1));
114     assertEquals(0, Bytes.lastIndexOf(ARRAY234, (byte) 2));
115     assertEquals(1, Bytes.lastIndexOf(ARRAY234, (byte) 3));
116     assertEquals(2, Bytes.lastIndexOf(ARRAY234, (byte) 4));
117     assertEquals(3, Bytes.lastIndexOf(
118         new byte[] { (byte) 2, (byte) 3, (byte) 2, (byte) 3 },
119         (byte) 3));
120   }
121 
122   public void testConcat() {
123     assertTrue(Arrays.equals(EMPTY, Bytes.concat()));
124     assertTrue(Arrays.equals(EMPTY, Bytes.concat(EMPTY)));
125     assertTrue(Arrays.equals(EMPTY, Bytes.concat(EMPTY, EMPTY, EMPTY)));
126     assertTrue(Arrays.equals(ARRAY1, Bytes.concat(ARRAY1)));
127     assertNotSame(ARRAY1, Bytes.concat(ARRAY1));
128     assertTrue(Arrays.equals(ARRAY1, Bytes.concat(EMPTY, ARRAY1, EMPTY)));
129     assertTrue(Arrays.equals(
130         new byte[] {(byte) 1, (byte) 1, (byte) 1},
131         Bytes.concat(ARRAY1, ARRAY1, ARRAY1)));
132     assertTrue(Arrays.equals(
133         new byte[] {(byte) 1, (byte) 2, (byte) 3, (byte) 4},
134         Bytes.concat(ARRAY1, ARRAY234)));
135   }
136 
137   public void testEnsureCapacity() {
138     assertSame(EMPTY, Bytes.ensureCapacity(EMPTY, 0, 1));
139     assertSame(ARRAY1, Bytes.ensureCapacity(ARRAY1, 0, 1));
140     assertSame(ARRAY1, Bytes.ensureCapacity(ARRAY1, 1, 1));
141     assertTrue(Arrays.equals(
142         new byte[] {(byte) 1, (byte) 0, (byte) 0},
143         Bytes.ensureCapacity(ARRAY1, 2, 1)));
144   }
145 
146   public void testEnsureCapacity_fail() {
147     try {
148       Bytes.ensureCapacity(ARRAY1, -1, 1);
149       fail();
150     } catch (IllegalArgumentException expected) {
151     }
152     try {
153       // notice that this should even fail when no growth was needed
154       Bytes.ensureCapacity(ARRAY1, 1, -1);
155       fail();
156     } catch (IllegalArgumentException expected) {
157     }
158   }
159 
160   public void testToArray() {
161     // need explicit type parameter to avoid javac warning!?
162     List<Byte> none = Arrays.<Byte>asList();
163     assertTrue(Arrays.equals(EMPTY, Bytes.toArray(none)));
164 
165     List<Byte> one = Arrays.asList((byte) 1);
166     assertTrue(Arrays.equals(ARRAY1, Bytes.toArray(one)));
167 
168     byte[] array = {(byte) 0, (byte) 1, (byte) 0x55};
169 
170     List<Byte> three = Arrays.asList((byte) 0, (byte) 1, (byte) 0x55);
171     assertTrue(Arrays.equals(array, Bytes.toArray(three)));
172 
173     assertTrue(Arrays.equals(array, Bytes.toArray(Bytes.asList(array))));
174   }
175 
176   public void testToArray_threadSafe() {
177     for (int delta : new int[] { +1, 0, -1 }) {
178       for (int i = 0; i < VALUES.length; i++) {
179         List<Byte> list = Bytes.asList(VALUES).subList(0, i);
180         Collection<Byte> misleadingSize =
181             Helpers.misleadingSizeCollection(delta);
182         misleadingSize.addAll(list);
183         byte[] arr = Bytes.toArray(misleadingSize);
184         assertEquals(i, arr.length);
185         for (int j = 0; j < i; j++) {
186           assertEquals(VALUES[j], arr[j]);
187         }
188       }
189     }
190   }
191 
192   public void testToArray_withNull() {
193     List<Byte> list = Arrays.asList((byte) 0, (byte) 1, null);
194     try {
195       Bytes.toArray(list);
196       fail();
197     } catch (NullPointerException expected) {
198     }
199   }
200 
201   public void testToArray_withConversion() {
202     byte[] array = {(byte) 0, (byte) 1, (byte) 2};
203 
204     List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2);
205     List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2);
206     List<Integer> ints = Arrays.asList(0, 1, 2);
207     List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2);
208     List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2);
209     List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2);
210 
211     assertTrue(Arrays.equals(array, Bytes.toArray(bytes)));
212     assertTrue(Arrays.equals(array, Bytes.toArray(shorts)));
213     assertTrue(Arrays.equals(array, Bytes.toArray(ints)));
214     assertTrue(Arrays.equals(array, Bytes.toArray(floats)));
215     assertTrue(Arrays.equals(array, Bytes.toArray(longs)));
216     assertTrue(Arrays.equals(array, Bytes.toArray(doubles)));
217   }
218 
219   public void testAsList_isAView() {
220     byte[] array = {(byte) 0, (byte) 1};
221     List<Byte> list = Bytes.asList(array);
222     list.set(0, (byte) 2);
223     assertTrue(Arrays.equals(new byte[] {(byte) 2, (byte) 1}, array));
224     array[1] = (byte) 3;
225     assertEquals(Arrays.asList((byte) 2, (byte) 3), list);
226   }
227 
228   public void testAsList_toArray_roundTrip() {
229     byte[] array = { (byte) 0, (byte) 1, (byte) 2 };
230     List<Byte> list = Bytes.asList(array);
231     byte[] newArray = Bytes.toArray(list);
232 
233     // Make sure it returned a copy
234     list.set(0, (byte) 4);
235     assertTrue(Arrays.equals(
236         new byte[] { (byte) 0, (byte) 1, (byte) 2 }, newArray));
237     newArray[1] = (byte) 5;
238     assertEquals((byte) 1, (byte) list.get(1));
239   }
240 
241   // This test stems from a real bug found by andrewk
242   public void testAsList_subList_toArray_roundTrip() {
243     byte[] array = { (byte) 0, (byte) 1, (byte) 2, (byte) 3 };
244     List<Byte> list = Bytes.asList(array);
245     assertTrue(Arrays.equals(new byte[] { (byte) 1, (byte) 2 },
246         Bytes.toArray(list.subList(1, 3))));
247     assertTrue(Arrays.equals(new byte[] {},
248         Bytes.toArray(list.subList(2, 2))));
249   }
250 
251   public void testAsListEmpty() {
252     assertSame(Collections.emptyList(), Bytes.asList(EMPTY));
253   }
254 }
255